function [ time_QR, P ] = QR_Perm( C, P0, row_num )

[N,d] = size(P0);
n = N/d;

iter_max = 30;
tol = 1e-1;

time_start = tic;

C = C ./ kron(sqrt(row_num*row_num'),ones(d));

Q_last = zeros(N,d);
Q = P0;
iter = 1;

while ( norm(Q-Q_last, 2) > tol && iter <= iter_max )
    Q_last = Q;
    Q = C * Q_last;
    tic
    [Q,~] = qr(Q,0);
    fprintf('QR iter = %d,  QR_fac Time = %f\n', iter, toc);
    iter = iter + 1;
end

Q = Q * Q(1:d,:)';
P = zeros(n*d,d);
for i = 1:n
    P((i-1)*d+1:i*d,:) = munkres( -Q((i-1)*d+1:i*d,:) );
end
P = P * P(1:d,:)';
time_QR = toc(time_start);

end